export default {
  namespaced: true,
  state: {
    // 是否深色模式
    active: false
  },
  
  actions: {
    /**
     * @description 激活一个主题
     * @param {String} themeValue 需要激活的主题名称
     */
    async set ({ state, commit, dispatch }, active) {
      // 将 vuex 中的主题应用到 dom
      commit('set',active)
      state.active = active
      // 持久化
      await dispatch('admin/db/set', {
        dbName: 'sys',
        path: 'theme.active',
        value: active,
        user: true
      }, { root: true })
    },
    /**
     * @description 从持久化数据加载主题设置     * @param {Object} context
     */
    async load ({ state, commit, dispatch }) {
      // store 赋值
      const active = await dispatch('admin/db/get', {
        dbName: 'sys',
        path: 'theme.active',
        defaultValue: state.active,
        user: true
      }, { root: true })
      state.active = active
      // 将 vuex 中的主题应用到 dom
      dispatch('set', active)
    }
  },
  mutations: {
    set (state, active) {
      state.active = active
    }
  }
}
